/**
 * 全选功能
 */
//获取全选按钮
var allBtn = document.querySelectorAll('.allBtn input')
var allBtn_one = document.querySelector('#allone')
var allBtn_two = document.querySelector('#alltwo')
var waresChoiceList = document.querySelectorAll('.waresChoice')

//获取结算数据
var piece = document.querySelector('.xz .piece')
var sum = document.querySelector('.xz .sum')
// console.log(waresChoiceList);
//获取所有选项按钮
var btnList = document.querySelectorAll('.oneBtn input')

allBtn_one.onclick = function () {
    if (this.checked) {
        for (let i = 0; i < btnList.length; i++) {
            btnList[i].checked = true
            waresChoiceList[i].style.backgroundColor = 'rgb(255, 244, 232)'
        }
        piece_sum()
    } else {
        for (let i = 0; i < btnList.length; i++) {
            btnList[i].checked = false
            waresChoiceList[i].style.backgroundColor = ''
        }
        piece.innerText = '0'
        sum.innerText = '0.00'
    }
    allBtn_two.checked = this.checked

}
allBtn_two.onclick = function () {
    if (this.checked) {
        for (let i = 0; i < btnList.length; i++) {
            btnList[i].checked = true
            waresChoiceList[i].style.backgroundColor = 'rgb(255, 244, 232)'
        }
        piece_sum()
    } else {
        for (let i = 0; i < btnList.length; i++) {
            btnList[i].checked = false
            waresChoiceList[i].style.backgroundColor = ''
        }
        piece.innerText = '0'
        sum.innerText = '0.00'
    }
    allBtn_one.checked = this.checked
}

//点击选择按钮
for (let i = 0; i < btnList.length; i++) {
    btnList[i].onclick = function () {
        // console.log(this.checked);
        if (this.checked) {
            waresChoiceList[i].style.backgroundColor = 'rgb(255, 244, 232)'
            piece_sum()
        } else {
            waresChoiceList[i].style.backgroundColor = ''
            piece_sum()
        }
        var temp = true
        // 判断 列表选择框 是否所有的全选择
        for (var j = 0; j < btnList.length; j++) {
            if (!btnList[j].checked) {
                // 只要出现一个 未选择 全选框就 不选中
                temp = false
            }
        }
        // 设置全选的选择状态
        allBtn_one.checked = temp
        allBtn_two.checked = temp
    }
}


/**
 * 数量加减计算
 */
//获取加减按钮
var reduceList = document.querySelectorAll('.reduce')
var plusList = document.querySelectorAll('.plus')
var numList = document.querySelectorAll('.number')
//获取单价
var onePriceList = document.querySelectorAll('.onePrice span')
//获取小计
var subTotalList = document.querySelectorAll('.subTotal span')
// console.log(onePriceList);
for (let i = 0; i < numList.length; i++) {
    //减
    reduceList[i].onclick = function () {
        let onePrice = Number(onePriceList[i].innerText)
        let subTotal = Number(subTotalList[i].innerText)
        let number = Number(numList[i].value)
        // console.log(number);
        if (number == 1) {
            numList[i].value = '1'
            subTotalList[i].innerText = Math.floor(onePrice * 100) /100
            piece_sum()
        } else {
            number--
            numList[i].value = number
            // console.log(subTotal,onePrice);
            subTotalList[i].innerText = Math.floor((subTotal - onePrice) * 100) /100
            piece_sum()
        }
    }
    //加
    plusList[i].onclick = function () {
        let onePrice = Number(onePriceList[i].innerText)
        let subTotal = Number(subTotalList[i].innerText)
        let number = Number(numList[i].value)
        // console.log(number);
        number++
        numList[i].value = number
        subTotalList[i].innerText = Math.floor((subTotal + onePrice) * 100) /100
        piece_sum()
    }
}

/**
 * 点击删除功能
 */
//获取删除按钮
var deleList = document.querySelectorAll('.operation')
for (let i = 0; i < deleList.length; i++) {
    deleList[i].onclick = function () {
        deleList[i].parentNode.parentNode.removeChild(deleList[i].parentNode);
        piece_sum()
    }
}
//删除选中的商品
var operationWares = document.querySelector('.operationWares')
operationWares.onclick = function () {
    let btnList = document.querySelectorAll('.oneBtn input')
    let waresChoiceList = document.querySelectorAll('.waresChoice')
    for (let i = 0; i < btnList.length; i++) {
        if (btnList[i].checked) {
            waresChoiceList[i].parentNode.removeChild(waresChoiceList[i])
            piece_sum()
        }
    }
}
//清空购物车
var emptyWares = document.querySelector('.emptyWares')
emptyWares.onclick = function () {
    let waresChoiceList = document.querySelectorAll('.waresChoice')
    for (let i = 0; i < waresChoiceList.length; i++) {
        waresChoiceList[i].parentNode.removeChild(waresChoiceList[i])
        piece_sum()
    }
}

/**
 * 总数结算的实现方法
 */
//获取商品总数和总价
function piece_sum() {
    let numList = document.querySelectorAll('.number')
    let subTotalList = document.querySelectorAll('.subTotal span')
    let btnList = document.querySelectorAll('.oneBtn input')

    let num = 0, subTotal = 0;
    for (let i = 0; i < numList.length; i++) {
        if (btnList[i].checked) {
            num += Number(numList[i].value)
            subTotal += Number(subTotalList[i].innerText)
        }
    }
    piece.innerText = num
    sum.innerText = Math.floor(subTotal * 100) /100
}
piece_sum()
